/* 
 * File:   Ventana.cpp
 * Author: agu
 * 
 * Created on 17 de mayo de 2014, 19:39
 */
#include <iostream>
#include "client_Window.h"
#include "client_Color.h"
#include "SDL2/SDL_image.h"
using std::cout;
using std::endl;

Window::Window() {

}

Window::Window(string title) {
    //The window we'll be rendering to
    this->window = NULL;
    //The surface contained by the window
    this->title = title;
    this->width = this->SCREEN_DEFAULT_WIDTH;
    this->height = this->SCREEN_DEFAULT_HEIGHT;
    this->active = true;
}

Window::Window(string title, int width, int height) {
    //The window we'll be rendering to
    this->window = NULL;
    //The surface contained by the window
    this->title = title;
    this->width = width;
    this->height = height;
    this->active = true;
}

Window::~Window() {
    //Destroy window
    SDL_DestroyWindow(this->window);
    this->window = NULL;
}

bool Window::init() {
    //Initialization flag
    bool success = true;

    //Create window
    this->window = SDL_CreateWindow(this->title.c_str(),
            SDL_WINDOWPOS_UNDEFINED,
            SDL_WINDOWPOS_UNDEFINED,
            this->width,
            this->height,
            //SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
            SDL_WINDOW_SHOWN);
    if (this->window == NULL) {
        cout << "Window could not be created! SDL_Error: " << SDL_GetError()
                << endl;
        success = false;
    }

    return success;
}

bool Window::init(SDL_Renderer* renderer) {
    //Initialization flag
    bool success = true;
    if (SDL_CreateWindowAndRenderer(this->width,
            this->height,
            SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL,
            &this->window, &renderer) < 0) {
        cout << "Window could not be created! SDL_Error: " << SDL_GetError()
                << endl;
        success = false;
    }
    SDL_SetWindowTitle(this->window, this->title.c_str());

    return success;
}

SDL_Window* Window::getWindowSDL() {
    return this->window;
}

int Window::getWidth() {
    return this->width;
}

int Window::getHeight() {
    return this->height;
}

void Window::handleEvent(SDL_WindowEvent windowEvent) {
    //Get new dimensions and repaint on window size change

    if (windowEvent.event == SDL_WINDOWEVENT_CLOSE) {
        this->kill();
    }
    if (windowEvent.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
        this->width = windowEvent.data1;
        this->height = windowEvent.data2;
    }

}

string Window::getTitle() {
    return this->title;
}

bool Window::isActive() {
    return this->active;
}

void Window::kill() {
    this->active = false;
}

void Window::setWidth(int width) {
    this->width = width;
    SDL_SetWindowSize(this->window, this->width, this->height);
}

void Window::setHeight(int height) {
    this->height = height;
    SDL_SetWindowSize(this->window, this->width, this->height);
}

void Window::close() {
    SDL_DestroyWindow(this->window);
}